﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zzva.Common;
using System.Collections.ObjectModel;
using Zzva.DomainObjectReg;
using System.Collections;

namespace Zzva.UIWin
{
    public partial class TFrmFilterByValue : TFrmDialBase 
    {

        TRegDirBase mvarRegDirBase;



        public TFrmFilterByValue(TRegDirBase pRegDirBase, Collection<string> pListFieldAlias,
                                 string pFieldAliasDefault)
            : base()
        {
            try
            {

                string lId = "Id";


                InitializeComponent();

                mvarRegDirBase = pRegDirBase;


                Collection<string> lListFieldAliasWithoutId = new Collection<string>();


                foreach (string lItem in pListFieldAlias)
                {
                    if (lItem != lId) { lListFieldAliasWithoutId.Add(lItem); }
                }

                if (lListFieldAliasWithoutId.Count <= 0) throw (new CommonException("Отсутсвуют поля для фильтрации"));



                lstFieldAlias.DataSource = lListFieldAliasWithoutId;


                if ((pFieldAliasDefault != lId) & (pFieldAliasDefault != ""))
                {
                    lstFieldAlias.Text = pFieldAliasDefault;
                }



                UpdateListFieldValue();


            }

            catch (Exception e1) { throw e1; }
            finally { }
        }



















































































        private void lstFilterValue_TextChanged(object sender, EventArgs e)
        {
            ////lstFilterValue.DroppedDown = true;
        }

        private void lstFilterValue_Enter(object sender, EventArgs e)
        {
            ////lstFilterValue.DroppedDown = true;
        }


        private void lstFieldAlias_TextChanged(object sender, EventArgs e)
        {
            UpdateListFieldValue();
        }
 


        public TFrmFilterByValue(TRegDirBase pRegDirBase, Collection<string>  pListFieldAlias)
            : base()
        {
            try
            {
                InitializeComponent();

                mvarRegDirBase = pRegDirBase;


                Collection<string> lListFieldAliasWithoutId = new Collection<string>();


                foreach (string  lItem in pListFieldAlias)
                {
                    if (lItem != "Id") { lListFieldAliasWithoutId.Add(lItem); }
                }

                if (lListFieldAliasWithoutId.Count <= 0) throw (new CommonException("Отсутсвуют поля для фильтрации"));



                lstFieldAlias.DataSource = lListFieldAliasWithoutId;


                UpdateListFieldValue();




            }

            catch (Exception e1) { throw e1; }
            finally { }
        }

      

        //Обновить список значений текущего поля
        private void UpdateListFieldValue()
        {
            try
            {
                string lAlias;
                lAlias = lstFieldAlias.Text;
                if (lAlias == "") { throw (new CommonException("Не задано имя поля")); }

                Collection<string> lListValue = mvarRegDirBase.GetListUniqueValueForAliasField(lAlias);


                lstFilterValue.DataSource = lListValue;
          

            }

            catch (Exception e1) { MessageBox.Show(e1.Message); }
            finally { }
        }



        ////public TFrmFilterByValue()
        ////{
        ////    InitializeComponent();
        ////}

 




        protected override void Ready()
        {
            try
            {
                string lAlias = lstFieldAlias.Text.Trim() ;
                string lFilterValue = lstFilterValue.Text.Trim() ;

                if (lAlias == "") { throw (new CommonException("Не задано имя поля")); }
                if (lFilterValue == "") { throw (new CommonException("Не задано значение фильтра")); }

                Collection<TFieldValue> lListFieldValue = new Collection<TFieldValue>(); 

                TFieldValue lFieldValue;
                lFieldValue = new TFieldValue("Alias", lAlias);
                lListFieldValue.Add(lFieldValue);

                lFieldValue = new TFieldValue("FilterValue", lFilterValue);
                lListFieldValue.Add(lFieldValue);

                TListFieldValueEventArgs lListFieldValueEventArgs = new TListFieldValueEventArgs(lListFieldValue);  

                FireEventReady(lListFieldValueEventArgs);

                this.Close();
            }

            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally { }
        }





 

 







    }
}
